package com.notice.view;

import com.notice.model.MessageModel;
import com.notice.model.PhotoModel;
import com.notice.model.FileModel;
import com.notice.viewmodel.MessageViewModel;
import com.notice.util.ResponseUtil;
import com.notice.common.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.PostConstruct;
import java.util.Observable;
import java.util.Observer;

@Controller
@RequestMapping("/Message")
@CrossOrigin
public class MessageView implements Observer {

    @Autowired
    private MessageViewModel messageViewModel;
    
    @PostConstruct
    public void init() {
        messageViewModel.addObserver(this);
    }

    @PostMapping("/Add")
    @ResponseBody
    public ResponseEntity<?> addMessage(@RequestBody MessageModel message) {
        Integer mid = messageViewModel.addMessage(message);
        if(mid != null) {
            return ResponseUtil.success(mid);
        }
        return ResponseUtil.error(Constants.ErrorCodes.LOGIN_FAIL, Constants.Messages.OPERATION_FAIL);
    }
    
    @PostMapping("/Search")
    @ResponseBody
    public ResponseEntity<?> searchMessages(@RequestBody MessageModel query) {
        return ResponseUtil.success(messageViewModel.searchMessages(query));
    }
    
    @PostMapping("/GetDetail")
    @ResponseBody
    public ResponseEntity<?> getDetail(@RequestBody MessageModel message) {
        Object detail = messageViewModel.getMessageDetail(message.getMid());
        if (detail != null) {
            return ResponseUtil.success(detail);
        }
        return ResponseUtil.error(Constants.ErrorCodes.LOGIN_FAIL, Constants.Messages.OPERATION_FAIL);
    }
    
    @PostMapping("/Delete/{mid}")
    @ResponseBody
    public ResponseEntity deleteMessage(@PathVariable Integer mid) {
        if(messageViewModel.deleteMessage(mid)) {
            return ResponseUtil.success("success");
        }
        return ResponseUtil.error(Constants.ErrorCodes.DELETE_FAIL, Constants.Messages.OPERATION_FAIL);
    }
    
    @PostMapping("/Update")
    @ResponseBody
    public ResponseEntity updateMessage(@RequestBody MessageModel message) {
        if(messageViewModel.updateMessage(message)) {
            return ResponseUtil.success("success");
        }
        return ResponseUtil.error(Constants.ErrorCodes.UPDATE_FAIL, Constants.Messages.UPDATE_FAIL);
    }
    
    @PostMapping("/photoup")
    @ResponseBody
    public ResponseEntity uploadPhoto(@RequestBody PhotoModel photo) {
        if(messageViewModel.addPhoto(photo)) {
            return ResponseUtil.success("success");
        }
        return ResponseUtil.error(Constants.ErrorCodes.LOGIN_FAIL, Constants.Messages.OPERATION_FAIL);
    }
    
    @PostMapping("/fileup")
    @ResponseBody
    public ResponseEntity uploadFile(@RequestBody FileModel file) {
        if(messageViewModel.addFile(file)) {
            return ResponseUtil.success();
        }
        return ResponseUtil.error(Constants.ErrorCodes.LOGIN_FAIL, Constants.Messages.OPERATION_FAIL);
    }
    
    @PostMapping("/filechange")
    @ResponseBody
    public ResponseEntity changeFile(@RequestBody FileModel file) {
        if(messageViewModel.updateFile(file)) {
            return ResponseUtil.success();
        }
        return ResponseUtil.error(Constants.ErrorCodes.LOGIN_FAIL, Constants.Messages.OPERATION_FAIL);
    }
    
    @PostMapping("/photochange")
    @ResponseBody
    public ResponseEntity changePhoto(@RequestBody PhotoModel photo) {
        if(messageViewModel.updatePhoto(photo)) {
            return ResponseUtil.success();
        }
        return ResponseUtil.error(Constants.ErrorCodes.LOGIN_FAIL, Constants.Messages.OPERATION_FAIL);
    }
    
    @PostMapping("/filedelete")
    @ResponseBody
    public ResponseEntity deleteFile(@RequestBody FileModel file) {
        if(messageViewModel.deleteFile(file)) {
            return ResponseUtil.success("success");
        }
        return ResponseUtil.error(Constants.ErrorCodes.DELETE_FAIL, Constants.Messages.OPERATION_FAIL);
    }
    
    @PostMapping("/photodelete")
    @ResponseBody
    public ResponseEntity deletePhoto(@RequestBody PhotoModel photo) {
        if(messageViewModel.deletePhoto(photo)) {
            return ResponseUtil.success("success");
        }
        return ResponseUtil.error(Constants.ErrorCodes.DELETE_FAIL, Constants.Messages.OPERATION_FAIL);
    }
    
    @PostMapping("/filefind")
    @ResponseBody
    public ResponseEntity findFiles(@RequestBody FileModel file) {
        System.out.println(findFiles(file).toString());
        return ResponseUtil.success(messageViewModel.findFiles(file));
    }
    
    @PostMapping("/photofind")
    @ResponseBody
    public ResponseEntity findPhotos(@RequestBody PhotoModel photo) {
        return ResponseUtil.success(messageViewModel.findPhotos(photo));
    }
    
    // ... 其他文件和图片相关的接口
    
    @Override
    public void update(Observable o, Object arg) {
        // 处理 ViewModel 的状态更新
        // 可以在这里处理消息状态的变化，比如实时更新等
        System.out.println("Message state updated: " + arg);
    }
} 